/**
 * @param {number[][]} grid
 * @return {number}
 */
var maxIncreaseKeepingSkyline = function(grid) {
  let maxCol = [], maxRow = []
  let m = grid.length, n = grid[0].length
  for(let i = 0; i < m; i++) {
    maxRow.push(Math.max(...grid[i]))
  }
  for(let i = 0; i < n; i++) {
    let tempMax = 0
    for(let j = 0; j < m; j++) {
      tempMax = Math.max(tempMax, grid[j][i])
    }
    maxCol.push(tempMax)
  }
  let ans = 0
  for(let i = 0; i < m; i++) {
    for(let j = 0; j < n; j++) {
      if(grid[i][j] <= maxRow[i] && grid[i][j] <= maxCol[j]) {
        ans += Math.min(maxRow[i], maxCol[j]) - grid[i][j]
      }
    }
  }
  return ans
};